const Order = require('../model/Order')
const User = require('../model/User')
const svgCaptcha = require('svg-captcha')
const moment = require('moment')

//成功返回
const successRender = ({code = '200', msg = '操作成功', data = {}} = {}) => {
  let returnData = {
    code,
    success: true,
    msg,
    data
  }
  return returnData
}

//失败返回
const errorRender = ({code = '500', msg = '操作失败', data = {}} = {}) => {
  let returnData = {
    code,
    success: false,
    msg,
    data
  }
  return returnData
}

//生成订单号
const generateOrderId = (j) => {
  let random_no = "";
  for (let i = 0; i < j; i++) //j位随机数，用以加在时间戳后面。
  {
    random_no += Math.floor(Math.random() * 10)
  }
  random_no = new Date().getTime() + random_no
  return random_no
}

//生成取餐号
const generatePickUpNum = async () => {
  let pickUpNum = ''
  let numArr = []
  const res = await Order.find()
  const conduct = res.filter(item => item.status == 0)
  if (conduct.length === 0) {
    pickUpNum = 'A001'
    return pickUpNum
  }
  conduct.forEach(item => {
    let num = parseInt(item.pickUpNum.substr(1))
    numArr.push(num)
  })
  let orderNum = Math.max(...numArr)
  orderNum++
  if (orderNum >= 10 && orderNum < 100) orderNum = '0' + orderNum
  else if (orderNum > 0 && orderNum < 10) orderNum = '00' + orderNum
  pickUpNum = 'A' + orderNum
  return pickUpNum
}

//图片验证码
const getCaptcha = () => {
  const captcha = svgCaptcha.create({
    size: 4,
    ignoreCharsL: 'o01i',
    noise: 1,
    color: true,
    background: '#fff'
  })
  return captcha;
}

//分页过滤
const paginationFilter = (list, obj) => {
  obj.pageSize = parseInt(obj.pageSize)
  obj.pageNum = parseInt(obj.pageNum)
  const total = list.length
  const count = (obj.pageNum - 1) * obj.pageSize
  let arr = []
  list.forEach((item, index) => {
    if (index >= count && index < count + obj.pageSize) {
      arr.push(item)
    }
  })
  return {
    total,
    pageNum: obj.pageNum,
    pageSize: obj.pageSize,
    list: arr
  }
}

//总收入
const getIncome = async () => {
  const res = await Order.find()
  let income = 0
  res.forEach(item => {
    income += item.price
  })
  return income
}

//翻台率
const getTurnoverRate = async () => {
  const res = await Order.find()
  return (res.length / 3).toFixed(2)
}

//本月新增会员
const getNewMember = async () => {
  const res = await User.find()
  let num = 0
  res.forEach(item => {
    const month = moment(item.createTime).month()
    const now = moment().month()
    if (month == now) num++
  })
  return num
}

//累计订单总数
const cumulativeOrder = async () => {
  let orderNum = [0, 0, 0, 0, 0]
  const res = await Order.find()
  const timeArr = [12, 14, 16, 18, 20]
  timeArr.forEach((value, index) => {
    res.forEach(item => {
      const hour = moment(item.createTime).hour()
      if (hour >= value && hour <= value + 2) {
        orderNum[index] = orderNum[index] + 1
      }
    })
  })
  return orderNum
}

//累计销售总数
const cumulativePrice = async () => {
  let priceNum = [0, 0, 0, 0, 0]
  const res = await Order.find()
  const timeArr = [12, 14, 16, 18, 20]
  timeArr.forEach((value, index) => {
    res.forEach(item => {
      const hour = moment(item.createTime).hour()
      if (hour >= value && hour <= value + 2) {
        priceNum[index] = priceNum[index] + item.price
      }
    })
  })
  return priceNum
}

module.exports = {
  errorRender,
  successRender,
  generateOrderId,
  generatePickUpNum,
  getCaptcha,
  paginationFilter,
  getIncome,
  getTurnoverRate,
  getNewMember,
  cumulativeOrder,
  cumulativePrice
}

